package com.kitty.game.fuling;

import com.kitty.common.utils.SpringUtils;
import com.kitty.common.thread.IdGenerator;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.friend.facade.FriendController;
import com.kitty.game.fuling.model.Fuling;
import com.kitty.game.hunpo.service.HunPoService;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.common.model.Reason;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class FulingController {
    static Logger logger = LoggerFactory.getLogger(FriendController.class);

    static EquipService equipService = (EquipService) SpringUtils.getBean(EquipService.class);

    static String[] zhenlingType = new String[]{"青龙", "白虎", "朱雀", "玄武"};

    static CountService countService = (CountService) SpringUtils.getBean(CountService.class);

    @Autowired
    protected PetService petService;

    @RequestMapping
    public void reqFulingData(Role role, ReqFulingData reqFulingData) {
        Fuling fuling = SpringUtils.getFulingService().getFulingdata(role.getRoleId());
        RespReqFuling respReqFuling = getRespReqFuling(fuling);
        MessagePusher.pushMessage(role, (Message) respReqFuling);
    }

    @RequestMapping
    public void reqActiveFulingzhen(Role role, ReqActiveFulingzhen reqActiveFulingzhen) {
        Fuling fuling = new Fuling();
        fuling.setJingyan(Integer.valueOf(0));
        fuling.setJieshu(Integer.valueOf(1));
        fuling.setJishu(Integer.valueOf(1));
        EquipService bean = SpringUtils.getBean(EquipService.class);
        int count1 = bean.getCount("铸灵石", true, role);
        fuling.setZhulingshi(Integer.valueOf(count1));
        fuling.setBaihu(Integer.valueOf(1));
        fuling.setQinglong(Integer.valueOf(1));
        fuling.setZhuque(Integer.valueOf(1));
        fuling.setXuanwu(Integer.valueOf(1));
        fuling.setRole_id(role.getRoleId());
        fuling.setType1((byte) 0);
        fuling.setType2((byte) 0);
        fuling.setType3((byte) 0);
        fuling.setType4((byte) 0);
        fuling.setWawa_id("null");
        fuling.setChongwu_id("null");
        fuling.setFushen1_id("null");
        fuling.setFushen2_id("null");
        fuling.setFushen3_id("null");
        int jingyan = fuling.getJingyan().intValue();
        int jieshu = fuling.getJieshu().intValue();
        int jishu = fuling.getJishu().intValue();
        RespReqFuling respReqFuling = new RespReqFuling();
        respReqFuling.setJingyan(jingyan ^ (17 + jieshu) * 16 * 16 + 17 + jishu);
        respReqFuling.setJieshu((byte) (jieshu ^ 0x22));
        respReqFuling.setJishu((byte) (jishu ^ jieshu));
        respReqFuling.setZhulingshi(fuling.getZhulingshi().byteValue());
        respReqFuling.setQinglong((byte) 1);
        respReqFuling.setBaihu((byte) 1);
        respReqFuling.setZhuque((byte) 1);
        respReqFuling.setXuanwu((byte) 1);
        List<Fusheninfo> fushenList = new ArrayList<>();
        respReqFuling.setList(fushenList);
        respReqFuling.setaByte((byte) 0);
        fuling.setId(Long.valueOf(IdGenerator.getNextId()));
        fuling.save();
        role.setQinglong(1.0);
        role.setBaihu(1.0);
        role.setZhuque(1.0);
        role.setXuanwu(1.0);
        role.setFulingLevel(1);
        role.save();
        SpringUtils.getBean(RoleService.class).addAchieveScore(role,"106003");
        SpringUtils.getRoleService().sendExtraAttribute(role);
        MessagePusher.pushMessage(role, (Message) respReqFuling);
    }

    @RequestMapping
    public void reqBuyzhulingshi(Role role, ReqBuyfulingshi reqBuyfulingshi) {
        RoleService roleService = (RoleService) SpringUtils.getBean(RoleService.class);
        Fuling fuling = SpringUtils.getFulingService().getFulingdata(role.getRoleId());
        if (role.getGold() > 2000 && fuling.getZhulingshi().intValue() < 999) {
            int flscount = fuling.getZhulingshi().intValue();
            SpringUtils.getRoleService().subtractGold(role, 2000, Reason.BUY_DEVIL_TIME);
            fuling.setZhulingshi(Integer.valueOf(flscount + 1));
            fuling.save();
            MessagePusher.pushMessage(role, (Message) getRespReqFuling(fuling));
        } else {
            RespMsg respInviteMsg = new RespMsg();
            respInviteMsg.setMsg("铸灵石数量达到上限或积分不足，购买失败！");
            MessagePusher.pushMessage(role, (Message) respInviteMsg);
        }
    }

    @RequestMapping
    public void reqApplyzhulingshi(Role role, ReqApplyZhulingshi reqApplyZhulingshi) {
        RespMsg respInviteMsg = new RespMsg();
        Fuling fuling = SpringUtils.getFulingService().getFulingdata(role.getRoleId());
        int zhulingshi = fuling.getZhulingshi().intValue();
        int jingyan = fuling.getJingyan().intValue();
        int jieshu = fuling.getJieshu().intValue();
        int jishu = fuling.getJishu().intValue();
        if (zhulingshi > 0 && jieshu <= 5) {
            jingyan += 3000;
            Map<String, Map<String, Integer>> map = FulingInfo.map;
            int exp = ((Integer) ((Map) map.get(jieshu + "-" + jishu)).get("exp")).intValue();
            if (jingyan > exp) {
                jishu++;
                fuling.setJishu(Integer.valueOf(jishu));
                jingyan -= exp;
                if (jishu > 10) {
                    jieshu++;
                    fuling.setJieshu(Integer.valueOf(jieshu));
                    jishu = 1;
                    fuling.setJishu(Integer.valueOf(jishu));
                }
                role.setFulingLevel(role.getFulingLevel() + 1);
                role.save();
                SpringUtils.getRoleService().sendExtraAttribute(role);
                respInviteMsg.setMsg("灵阵升级成功");
                MessagePusher.pushMessage(role, (Message) respInviteMsg);
            }
            if(jieshu == 5 && jishu == 10){
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"106002");
            }

            fuling.setJingyan(Integer.valueOf(jingyan));
            fuling.setZhulingshi(Integer.valueOf(zhulingshi - 1));
            equipService.delRoloEquip("铸灵石", 1, true, role);
            fuling.save();
            respInviteMsg.setMsg("铸灵石使用成功");
            MessagePusher.pushMessage(role, (Message) respInviteMsg);
            RespReqFuling respReqFuling = getRespReqFuling(fuling);
            MessagePusher.pushMessage(role, (Message) respReqFuling);
        } else {
            respInviteMsg.setMsg("铸灵石不足，使用失败！");
            MessagePusher.pushMessage(role, (Message) respInviteMsg);
        }
    }

    @RequestMapping
    public void reqImproveZhenlingLevel(Role role, ReqImproveZhenlingLevel reqImproveZhenlingLevel) {
        RespMsg respInviteMsg = new RespMsg();
        if (role.getLevel() < 60) {
            respInviteMsg.setMsg("#Y60级#n以上才可使用，道友努力升级吧！");
            MessagePusher.pushMessage(role, (Message) respInviteMsg);
            return;
        }
        HunPoService hunPoService = (HunPoService) SpringUtils.getBean(HunPoService.class);
        Fuling fuling = SpringUtils.getFulingService().getFulingdata(role.getRoleId());
        int type = reqImproveZhenlingLevel.type;
        double count = 0.0;
        int level = 0;
        String typeName = zhenlingType[type - 1];
        switch (type) {
            case 1:
                level = fuling.getQinglong().intValue();
                fuling.setQinglong(Integer.valueOf(level + 1));
                break;
            case 2:
                level = fuling.getBaihu().intValue();
                fuling.setBaihu(Integer.valueOf(level + 1));
                break;
            case 3:
                level = fuling.getZhuque().intValue();
                fuling.setZhuque(Integer.valueOf(level + 1));
                break;
            case 4:
                level = fuling.getXuanwu().intValue();
                fuling.setXuanwu(Integer.valueOf(level + 1));
                break;
        }
        if(level>=10){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"106005");
        }
        if(level>=50){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"106001");
        }
        count = Math.ceil((level / 7)) + 1.0;
        IoSession session = SessionUtils.getSessionBy(role.getUid().longValue());
        RoleEquip roleEquip = equipService.getRoleEquipByName(session, "真灵精粹");
        equipService.delEquipNum(role, roleEquip, (int) count);
        if (fuling.getQinglong().intValue() > 50)
            fuling.setQinglong(Integer.valueOf(50));
        if (fuling.getBaihu().intValue() > 50)
            fuling.setBaihu(Integer.valueOf(50));
        if (fuling.getZhuque().intValue() > 50)
            fuling.setZhuque(Integer.valueOf(50));
        if (fuling.getXuanwu().intValue() > 50)
            fuling.setXuanwu(Integer.valueOf(50));
        fuling.save();
        String msg = MessageFormat.format("你消耗了{0}个#Y真灵精粹#n将#R{1}真灵#n的等级提升到了#R{2}#n级。", new Object[]{Double.valueOf(count), typeName, Integer.valueOf(level + 1)});
        MessagePusher.pushMessage(role, (Message) new RespNotifyMiscEx(msg));
        MessagePusher.pushMessage(role, (Message) getRespReqFuling(fuling));
    }

    @RequestMapping
    public void ReqZhenlingFushen(Role role, ReqZhenlingFushen reqZhenlingFushen) {
        int extraMagPower;

        int objType = reqZhenlingFushen.objType;
        String objId = reqZhenlingFushen.objectId;
        byte type = reqZhenlingFushen.type;
        Fuling fuling = SpringUtils.getFulingService().getFulingdata(role.getRoleId());
        switch (objType) {
            case 1:
                setRoleFuLing(fuling, role, objId, type);
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"106004");
                break;
            case 2:
                setPetFuLing(fuling, role, objId, type);
                break;
            case 3:
                fuling.setType3(type);
                fuling.setType4((byte) 2);
                fuling.setFushen3_id(objId);
                break;
        }
        fuling.save();
        role.save();
        MessagePusher.pushMessage(role, (Message) getRespReqFuling(fuling));
    }
    public void setPetFuLing(Fuling fuling, Role role, String objId, byte type) {
        ArrayList<FiedValue> list = new ArrayList<>();
        RespUpdate respUpdate = new RespUpdate();
        fuling.setFushen2_id(objId);
        fuling.setType2(type);
        fuling.setType4((byte)3);
        String typename = zhenlingType[type - 1];
        Pet pet = SpringUtils.getPetService().getPetById(Integer.valueOf(objId).intValue(), role);
        pet.setZhenlingType(type);
        respUpdate.setRoleId(pet.getId());
        pet.setQinglong(1.0 + (0.001 * fuling.getQinglong()));
        pet.setBaihu(1.0 + (0.001 * fuling.getBaihu()));
        pet.setZhuque(1.0 + (0.001 * fuling.getZhuque()));
        pet.setXuanwu(1.0 + (0.001 * fuling.getXuanwu()));
        int level = 0;
        if (type == 1) {
            level = fuling.getQinglong().intValue();
            pet.setQinglong(1.0 + (0.002 * level));
            list.add(new FiedValue(1005, Integer.valueOf(1)));
        } else if (type == 2) {
            level = fuling.getQinglong().intValue();
            pet.setBaihu(1.0 + (0.002 * level));
            list.add(new FiedValue(1005, Integer.valueOf(2)));
        } else if (type == 3) {
            level = fuling.getQinglong().intValue();
            pet.setZhuque(1.0 + (0.002 * level));
            list.add(new FiedValue(1005, Integer.valueOf(3)));
        } else if (type == 4) {
            level = fuling.getQinglong().intValue();
            pet.setXuanwu(1.0 + (0.002 * level));
            list.add(new FiedValue(1005, Integer.valueOf(4)));
        }
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, (Message)respUpdate);
        String msg = MessageFormat.format("你的宠物成功被真灵#R{0}#n附身了。", new Object[]{typename});
        MessagePusher.pushMessage(role, (Message) new RespNotifyMiscEx(msg));
        SpringUtils.getPetService().refreshPetInfo(role, pet);


    }
    public void setRoleFuLing(Fuling fuling, Role role, String objId, byte type) {
        ArrayList<FiedValue> list = new ArrayList<>();
        RespUpdate respUpdate = new RespUpdate();
        int level = 0;
        String typename = zhenlingType[type - 1];
        fuling.setFushen1_id(objId);
        fuling.setType1(type);
        fuling.setType4((byte) 2);
        respUpdate.setRoleId(role.getRoleId());
        role.setQinglong(1.0 + (0.001 * fuling.getQinglong()));
        role.setBaihu(1.0 + (0.001 * fuling.getBaihu()));
        role.setZhuque(1.0 + (0.001 * fuling.getZhuque()));
        role.setXuanwu(1.0 + (0.001 * fuling.getXuanwu()));
        role.setZhenlingType(type);
        switch (type) {
            case 1:
                level = fuling.getQinglong().intValue();
                role.setQinglong(1.0 + (0.002 * level));
                list.add(new FiedValue(1005, Integer.valueOf(1)));
                break;
            case 2:
                level = fuling.getBaihu().intValue();
                role.setBaihu(1.0 + (0.002 * level));
                list.add(new FiedValue(1005, Integer.valueOf(2)));
                break;
            case 3:
                level = fuling.getZhuque().intValue();
                role.setZhuque(1.0 + (0.002 * level));
                list.add(new FiedValue(1005, Integer.valueOf(3)));
                break;
            case 4:
                level = fuling.getXuanwu().intValue();
                role.setXuanwu(1.0 + (0.002 * level));
                list.add(new FiedValue(1005, Integer.valueOf(4)));
                break;
        }
        String msg = MessageFormat.format("你的角色成功被真灵#R{0}#n附身了。", new Object[]{typename});
        MessagePusher.pushMessage(role, (Message) new RespNotifyMiscEx(msg));
        SpringUtils.getRoleService().sendExtraAttribute(role);
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, (Message) respUpdate);
    }

    @RequestMapping
    public void ReqCancelZhenling(Role role, ReqCancelZhenling reqCancelZhenling) {
        Pet pet;
        RespUpdate respUpdatePet;
        String msg1;
        int objType = reqCancelZhenling.type;
        String objId = reqCancelZhenling.id;
        Fuling fuling = SpringUtils.getFulingService().getFulingdata(role.getRoleId());
        RespUpdate respUpdate = new RespUpdate();
        String msg = "";
        ArrayList<FiedValue> list = new ArrayList<>();
        switch (objType) {
            case 1:
                String typename = zhenlingType[fuling.getType1() - 1];
                fuling.setType1((byte) 0);
                fuling.setFushen1_id("null");
                respUpdate.setRoleId(role.getRoleId());
                role.setQinglong(1.0);
                role.setBaihu(1.0);
                role.setZhuque(1.0);
                role.setXuanwu(1.0);
                role.setZhenlingType((byte) 0);
                list.add(new FiedValue(1005, Integer.valueOf(0)));
                if (fuling.getFushen2_id().equals("null"))
                    fuling.setType4((byte) 0);
                msg = MessageFormat.format("你成功取消了#R{0}真灵对#R你自己#n的附身。", new Object[]{typename});
                SpringUtils.getRoleService().sendExtraAttribute(role);
                respUpdate.setList(list);
                fuling.save();
                MessagePusher.pushMessage(role, (Message)respUpdate);
                break;
            case 2:
                fuling.setType2((byte)0);
                fuling.setFushen2_id("null");
                pet = role.getPetBox().getPetByPetId(Integer.parseInt(objId));
                String typename1 = zhenlingType[pet.getZhenlingType()-1];
                respUpdate = new RespUpdate();
                respUpdate.setRoleId(pet.getId());
                pet.setQinglong(1.0);
                pet.setBaihu(1.0);
                pet.setZhuque(1.0);
                pet.setXuanwu(1.0);

                msg = MessageFormat.format("你成功取消了#R{0}真灵对#R宠物#n的附身。", new Object[] { typename1 });
                list.add(new FiedValue(1005, Integer.valueOf(0)));
                respUpdate.setList(list);
                MessagePusher.pushMessage(role, (Message)respUpdate);
                pet.setZhenlingType((byte)0);
                if (fuling.getFushen1_id().equals("null"))
                    fuling.setType4((byte)0);
                fuling.save();
                this.petService.refreshPetInfo(role, pet);
                // MessagePusher.pushMessage(role, (Message)new RespNotifyMiscEx(msg1));
                break;
        }
        role.save();
        MessagePusher.pushMessage(role, (Message)new RespNotifyMiscEx(msg));
        MessagePusher.pushMessage(role, (Message)getRespReqFuling(fuling));
    }

    public static RespReqFuling getRespReqFuling(Fuling fuling) {
        RespReqFuling respReqFuling = new RespReqFuling();
        int jingyan = fuling.getJingyan().intValue();
        int jieshu = fuling.getJieshu().intValue();
        int jishu = fuling.getJishu().intValue();
        respReqFuling.setJingyan(jingyan ^ (17 + jieshu) * 16 * 16 + 17 + jishu);
        if (jieshu > 5) {
            respReqFuling.setJieshu((byte) 39);
            respReqFuling.setJishu((byte) 15);
            fuling.setJieshu(Integer.valueOf(5));
            fuling.setJishu(Integer.valueOf(10));
            fuling.save();
        } else {
            respReqFuling.setJieshu((byte) (jieshu ^ 0x22));
            respReqFuling.setJishu((byte) (jishu ^ jieshu));
        }
        int qinglong = fuling.getQinglong().intValue();
        int baihu = fuling.getBaihu().intValue();
        int zhuque = fuling.getZhuque().intValue();
        int xuanwu = fuling.getXuanwu().intValue();
        respReqFuling.setZhulingshi((byte) fuling.getZhulingshi().intValue());
        if (qinglong > 50)
            qinglong = 50;
        if (baihu > 50)
            baihu = 50;
        if (zhuque > 50)
            zhuque = 50;
        if (xuanwu > 50)
            xuanwu = 50;
        respReqFuling.setQinglong((byte) qinglong);
        respReqFuling.setBaihu((byte) baihu);
        respReqFuling.setZhuque((byte) zhuque);
        respReqFuling.setXuanwu((byte) xuanwu);
        String Fushen1_id = fuling.getFushen1_id();
        String Fushen2_id = fuling.getFushen2_id();
        String Fushen3_id = fuling.getFushen3_id();
        List<Fusheninfo> fushenList = new ArrayList<>();
        if (!Fushen1_id.equals("null")) {
            Fusheninfo fushen1 = new Fusheninfo();
            fushen1.Id = Fushen1_id;
            fushen1.isFushen = 1;
            fushen1.type = fuling.getType1();
            fushenList.add(fushen1);
        }
        int roleid = fuling.getRole_id();
        Role role = SpringUtils.getRoleService().getOnlinePlayer(roleid);
        List<Pet> pets = SpringUtils.getPetService().getPetList(role);
        for (int i=0; i<pets.size(); ++i)
        {
            Pet pet = pets.get(i);
            if (pet.getZhenlingType() != 0)
            {
                Fusheninfo fushen2 = new Fusheninfo();
                fushen2.Id = String.valueOf(pet.getId()) ;
                fushen2.isFushen = 2;
                fushen2.type = pet.getZhenlingType();
                fushenList.add(fushen2);
            }
        }
//        if (!Fushen2_id.equals("null")) {
//            Fusheninfo fushen2 = new Fusheninfo();
//            fushen2.Id = Fushen2_id;
//            fushen2.isFushen = 1;
//            fushen2.type = fuling.getType2();
//            fushenList.add(fushen2);
//        }
//        if (!Fushen3_id.equals("null")) {
//            Fusheninfo fushen3 = new Fusheninfo();
//            fushen3.Id = Fushen3_id;
//            fushen3.isFushen = 1;
//            fushen3.type = fuling.getType3();
//            fushenList.add(fushen3);
//        }
        respReqFuling.setList(fushenList);
        respReqFuling.setaByte(fuling.getType4());
        return respReqFuling;
    }
}
